﻿using System;
using Web7.Container.Models;
using Web7.Core.Security;
using Microsoft.Practices.Unity;
using System.Collections.Generic;
using Web7.Utilities;

namespace Web7.Container.Services
{
    public class AssistUserService : IAssistUserService<Guid>
    {
        [Dependency]
        public IUserService _UserService { get; set; }

        [Dependency]
        public IPermissionRecordService _PermissionRecordService { get; set; }

        public bool VerifyUserUnicity(string userName, string email)
        {
            var user = _UserService.Get(u => u.UserName == userName && u.Email == email);
            return (user == null);
        }

        public void SendChallengeEmail(IUser user, Func<string, string> createUrl)
        {
            /*throw new NotImplementedException();*/
            //TODO;
        }

        public IUser ValidateChallenge(string challengeToken)
        {
            /*throw new NotImplementedException();*/
            //TODO;
            return null;
        }

        public bool SendLostPasswordEmail(string usernameOrEmail, Func<string, string> createUrl)
        {
            /*throw new NotImplementedException();*/
            //TODO;
            return false;
        }

        public IUser ValidateLostPassword(string nonce)
        {
            /*throw new NotImplementedException();*/
            //TODO;
            return null;
        }

        public IUser Get(Guid id)
        {
            return _UserService.Get(user => user.Id == id) as IUser;
        }

        public IUser CreateUser(CreateUserParams createUserParams)
        {
            var user = new User();
            user.Id = SequentialGuid.NewGuid();
            user.UserName = createUserParams.Username;
            user.Email = createUserParams.Email;
            user.Password = EncryptManager.Encrypt(SymmetricEncryptType.TripleDES, createUserParams.Password);
            user.PasswordFormat = SymmetricEncryptType.TripleDES;

            user.Status = UserStatus.NotSet;
            user.EmailStatus = UserStatus.NotSet;

            user.CreationDate = DateTime.Now;

            return user;
        }

        public IEnumerable<PermissionRecord> GetPermissionsForUser(Guid id)
        {
            return _PermissionRecordService.Fetch(p => p.OwnerId == id);
        }
    }
}
